var usedIds = new Array();

function showAddUser(){
	$('#user_options').html(loading_img);
	$.ajax({
	   type: "POST",
	   url: "connector.php",
	   data: 'action=getUsers&sID='+aSID,
	   success: function(msg) {
			$('#user_options').html(msg);
	   }
	});
	showPopUp('select_user');
}

function addUser(id) {
	var user_id = $('#ajax_user').val();
	if($.inArray(parseInt(user_id), usedIds) == -1){
		$.ajax({
			   type: "POST",
			   url: "connector.php",
			   data: 'action=addUserToList&lID='+id+'&uID='+user_id+'&sID='+aSID,
			   success: function(msg) {
					if(msg == 'Error:102') {
						document.location = 'logout.php?action=force';
					} else {
						showMessage(addSuccess);
						$('#all_users').append(newUser[0]+id+newUser[1]+user_id+newUser[2]+msg+newUser[3]+user_id+newUser[4]+id+newUser[5]);
						usedIds.push(user_id);
					}
			   }
			});
	} else {
		showMessage(alreadyAdded);
	}
	closePopUp();
}

function deleteUser(id, lId){
	alert(lId);
	if($.inArray(parseInt(id), usedIds) > -1){
		$.ajax({
			   type: "POST",
			   url: "connector.php",
			   data: 'action=deleteUserFromList&lID='+lId+'&uID='+id+'&sID='+aSID,
			   success: function(msg) {
					if(msg == 'Error:102') {
						document.location = 'logout.php?action=force';
					}else if(msg == 'ok'){
						showMessage(deleteSuccess);
						$('#list_user_'+lId+'_'+id).remove();
						usedIds = jQuery.grep(usedIds, function(value) { return value != id;});
					} else {
						alert(msg);
					}
			   }
			});
	}
}